<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

/**
 * 任务管理逻辑
 */
class Task extends AdminBase
{
    
    /**
     * 获取任务列表
     */
    public function getTaskList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelTask->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 任务信息编辑
     */
    public function taskEdit($data = [])
    {
        
        $validate_result = $this->validateTask->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateTask->getError()];
        }
        
        $url = url('taskList');
        
        $result = $this->modelTask->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '学生' . $handle_text . '，name：' . $data['name']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelTask->getError()];
    }
    /**
     * 导出任务列表
     */
    public function exportTaskList($where = [], $field = true, $order = '')
    {
        
        $list = $this->getTaskList($where, $field, $order, false);
        
        $titles = "名称,完成度";
        $keys   = "name,degree";
        
        action_log('导出', '导出任务列表');
        
        export_excel($titles, $keys, $list, '任务列表');
    }
    
    /**
     * 获取任务列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        
        !empty($data['search_data']) && $where['t.name'] = ['like', '%'.$data['search_data'].'%'];
        
        if (!is_administrator()) {
            
            $task = session('task_info');
            
            if ($task['is_share_task']) {
                
                $ids = $this->getInheritTaskIds(TASK_ID);
                
                $ids[] = TASK_ID;
                
                $where['t.id'] = ['in', $ids];
                
            } else {
                
                $where['t.id'] = TASK_ID;
            }
        }
        
        return $where;
    }
    

    /**
     * 获取任务信息
     */
    public function getTaskInfo($where = [], $field = true)
    {
        
        return $this->modelTask->getInfo($where, $field);
    }
    
    /**
     * 任务删除
     */
    public function taskDel($where = [])
    {
        
        $result = $this->modelTask->deleteInfo($where);
        
        $result && action_log('删除', '学生删除' . '，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '删除成功'] : [RESULT_ERROR, $this->modelTask->getError()];
    }
}
